//
//  GVistorView.swift
//  sinaWeiBoAI
//
//  Created by Z.J on 16/3/20.
//  Copyright © 2016年 Z.J. All rights reserved.
//

import UIKit

// MARK: - 代理
// 按钮点击协议
// 需要继承NSObjectProtocol
protocol GVistorViewDelegate :NSObjectProtocol
{
    // 点击注册
    func vistorViewRegister()
    // 点击登录
    func vistorViewLogin()
    
}

class GVistorView: UIView {

 
    //MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
        
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 添加代理属性
    weak var vistorViewDelegate : GVistorViewDelegate?
    
    
    /**
     不同页面设置不同信息
     默认为首页,首页不需要调用
     parameter imageName: 图标名称
     parameter message:   消息文字
     */
    func setupVisitorInfo(imageName: String, message: String)
    {
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = message
        // 根据字体自适应
        messageLabel.sizeToFit()
        // 隐藏房子
        homeView.hidden = true
        // 遮盖放在最下面
        sendSubviewToBack(coverView)
    }
    
    /// 旋转动画
    func startRotationAnimation()
    {
        // 基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 旋转角度
        animation.toValue = 2 * M_PI
        // 持续时间
        animation.duration = 20
        // 动画次数
        animation.repeatCount = MAXFLOAT
        // 要加上这句,不然切换到其他tabBar或者退出到桌面在切回来动画就停止了
        animation.removedOnCompletion = false
        // layer添加动画 -->forkey 可以用来取出动画.用来判断
        iconView.layer.addAnimation(animation, forKey: "home")
    
    }
    
    //MARK: - 约束布局
    private func prepareUI()
    {
        // 设置背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(coverView)
        // 添加子控件
        addSubview(iconView)
        addSubview(homeView)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(messageLabel)
      
        
        // 设置约束布局
        
        // 使用自动约束一定要设置成false.
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        
        // iconView X方向
        // iconVIew 的 中心点X坐标 等于 self 的中心点X坐标 * 1 + 0
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // iconView Y方向
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 房子 中心点与iconView重合
        // homeView X方向
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // homeView Y方向
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息label. CenterX和iconView.CenterX重合.,宽度224
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // messageLabel 顶部 等于 iconView 底部 *1 +16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // messageLabel宽度 :如果不参照其他控件toItem填nil. attribute填: NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        // 注册按钮
        // 顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 左边和messageLabel的左边重合
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 宽度 100
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度 35
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        // 顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 左边和messageLabel的右边重合
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // 宽度 100
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度 35
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
 
        // 遮盖
        // Y 顶部和父控件顶部重合
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        // 底部和registerButton底部重合
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        // 宽度
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
    
    }
    
    //MARK: - 按钮的点击事件
    func vistorViewRegister()
    {
        // 代理执行定义好的代理方法
        vistorViewDelegate?.vistorViewRegister()
    }
    
    func vistorViewLogin()
    {
        vistorViewDelegate?.vistorViewLogin()
    }

    
    //MARK: - 懒加载界面UI控件
    
    /// 转轮
    private lazy var iconView :UIImageView =
    {
        let view = UIImageView()
        
        view.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return view
    }()
    
    /// 房子
    private lazy var homeView : UIImageView =
    {
        let view = UIImageView()
        
        view.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return view
    }()
    
   /// 文字标签
    private lazy var messageLabel : UILabel =
    {
        let label = UILabel()
        
        label.text = "关注一些人,看看有上面惊喜"
        // 居中
        label.textAlignment = NSTextAlignment.Center
        // 换行
        label.numberOfLines = 0
        // 设置字体大小
        label.font = UIFont.systemFontOfSize(15)
        
        return label
    }()
    
    /// 注册按钮
    private lazy var registerBtn : UIButton =
    {
        let buttun = UIButton()
        
        buttun.setTitle("注册", forState: UIControlState.Normal)
        buttun.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        buttun.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        buttun.addTarget(self, action:"vistorViewRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return buttun
            
    }()
    
    /// 登录按钮
    private lazy var loginBtn : UIButton =
    {
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "vistorViewLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
 
    /// 遮罩视图
    private lazy var coverView : UIImageView =
    {
        let view = UIImageView()
        
        view.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return view
        
    }()
    
    
 
}
